﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class TransportConditionsCollection : AbstractCollection
    {
        public List<TransportConditions> TransportConditionsList
        {
            get { return Entities.TransportConditions.ToList(); }
        }

        public new bool Update(object item, string userLogin)
        {
            var newObject = (TransportConditions) item;
            if (newObject == null)
                return false;


            var object4Update = GetItemByIdFromDatabase(newObject.GetKeyValue()) as TransportConditions;
            if (object4Update == null)
                return false;

            if (newObject.IdInternalCourse != null)
                if (newObject.IdInternalCourse > 0) object4Update.IdInternalCourse = newObject.IdInternalCourse;

            if (newObject.IdTrucksCapacity != null)
                if (newObject.IdTrucksCapacity > 0) object4Update.IdTrucksCapacity = newObject.IdTrucksCapacity;

            if (newObject.TransportCostRUR != null)
                if (newObject.TransportCostRUR > 0) object4Update.TransportCostRUR = newObject.TransportCostRUR;

            if (newObject.IdUnit != null) if (newObject.IdUnit > 0) object4Update.IdUnit = newObject.IdUnit;

            if (newObject.IdLegalEntity != null)
                if (newObject.IdLegalEntity > 0) object4Update.IdLegalEntity = newObject.IdLegalEntity;

            if (newObject.IdContract != null)
                if (newObject.IdContract > 0) object4Update.IdContract = newObject.IdContract;

            if (!string.IsNullOrEmpty(newObject.Comments)) object4Update.Comments = newObject.Comments;


            return base.Update(object4Update, userLogin);
        }

        #region Overrides of AbstractCollection

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            return new List<object>(TransportConditionsList);
        }

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            return typeof (TransportConditions);
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.TransportConditions.Find(itemId);
        }

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}